package controller;

import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.WlCategory;
import pojo.WlCommodity;
import service.WlCategoryService;
import sun.java2d.pipe.PixelDrawPipe;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WlCategorycontroller {
    @Resource(name="wlCategoryService")
    WlCategoryService wlCategoryService;

    public WlCategoryService getWlCommodityService() {
        return wlCategoryService;
    }

    public void setWlCommodityService(WlCategoryService wlCategoryService) {
        this.wlCategoryService = wlCategoryService;
    }
    @RequestMapping("wcagetAll")
    public String getAll(HttpServletRequest request, Integer page ){
        if(page==null){
            page=1;
        }
        int rows = wlCategoryService.getCount();

        int pageYs = rows%9==0?rows/9:rows/9+1;

        if(page>pageYs){
            page=pageYs;
            request.getSession().setAttribute("page", page);
        }else{
            if(page<=0){
                page=1;
            }
            request.getSession().setAttribute("page", page);
        }
        page = (page-1)*9;
        List<WlCategory> wa = wlCategoryService.getAll(page);
        Map<WlCategory , Object> map=new HashMap<WlCategory, Object>();
        for (WlCategory c:wa){
            Object Pname;
            if(null!=c.getParentid()){
                Pname= wlCategoryService.getPname(c.getParentid());
                map.put(c,Pname);
            }else{

                Pname="--";
                map.put(c,Pname);
            }

        }

        if(wa.size()>0){
            request.getSession().setAttribute("map", map);
            request.getSession().setAttribute("ddd", wa);
            request.getSession().setAttribute("pageYs", pageYs);
        }
        return "redirect:/table_data_tables.jsp";
    }

    @RequestMapping("wcadel")
    public String del(String id){
        wlCategoryService.del(id);
        return "wcagetAll";
    }
    @RequestMapping("getoneById")
    public String getoneById(HttpServletRequest request,String cateid){
        WlCategory wc=wlCategoryService.getoneById(cateid);
        String pname=wlCategoryService.getPname(wc.getParentid());
        Map<WlCategory,Object> map=new HashMap<WlCategory,Object>();
        map.put(wc,pname);
        request.getSession().setAttribute("getOneWc",map);
        return "update_wlcategory.jsp";
    }
}
